package org.nox.helloseries.rest;

import java.util.ArrayList;
import java.util.List;

import org.nox.helloseries.core.exceptions.TrackerSearchException;
import org.nox.helloseries.core.services.configuration.tracker.TrackerConfigurationService;
import org.nox.helloseries.core.services.recherche.TrackerSearchService;
import org.nox.helloseries.core.services.recherche.domain.RetourRechercheTracker;
import org.nox.helloseries.domain.Qualite;
import org.nox.helloseries.domain.Serie;
import org.nox.helloseries.domain.Tracker;
import org.nox.helloseries.domain.Version;
import org.nox.helloseries.factory.HelloSeriesServiceFactory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import restx.annotations.GET;
import restx.annotations.RestxResource;
import restx.factory.Component;
import restx.security.PermitAll;

import com.google.common.base.Optional;

/**
 * Hello world!
 */
@Component
@RestxResource
public class HelloSeriesSearchResource
{

    private static final Logger LOG = LoggerFactory.getLogger( HelloSeriesSearchResource.class );

    private TrackerSearchService trackerSearchService = HelloSeriesServiceFactory.createOrGetService( TrackerSearchService.class );

    private TrackerConfigurationService trackerConfigurationService = HelloSeriesServiceFactory.createOrGetService( TrackerConfigurationService.class );

    @GET( "/search/serie" )
    @PermitAll
    public List<Serie> searchSerieOnAllTracker( String nom, int saison, int episode, Optional<Qualite> qualite, Optional<Version> version )
    {
        Serie serieARechercher = new Serie();
        serieARechercher.setNom( nom );
        serieARechercher.setEpisode( episode );
        serieARechercher.setSaison( saison );

        // Gestion des paramètres facultatifs
        if ( qualite.isPresent() )
        {
            serieARechercher.setQualite( qualite.get() );
        }
        else
        {
            // Valeur par défaut
            serieARechercher.setQualite( Qualite._720P );
        }

        if ( version.isPresent() )
        {
            serieARechercher.setVersion( version.get() );
        }
        else
        {
            // Valeur par défaut
            serieARechercher.setVersion( Version.VOSTFR );
        }

        // Création de l'objet retour
        List<Serie> retour = new ArrayList<>();

        for ( Tracker tracker : trackerConfigurationService.getTrackersActifs() )
        {
            try
            {
                Serie retourSerieTracker = trackerSearchService.getSerieToBeDownloaded( serieARechercher, tracker );

                if ( retourSerieTracker != null )
                {
                    retour.add( retourSerieTracker );
                }
            }
            catch ( TrackerSearchException e )
            {
                LOG.error( "Erreur lors de la recherche des séries", e );
            }
        }

        return retour;
    }

    @GET( "/search/series" )
    @PermitAll
    public RetourRechercheTracker searchAll()
    {
        return trackerSearchService.searchAll();
    }
}